package pdautils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;
import android.content.ServiceConnection;
import android.content.ComponentName;

import com.huibao.hulibao.R;
import com.huibao.hulibao.utils.T;
import com.smartdevice.aidl.IZKCService;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import android.widget.Toast;

import common.MessageCenter;
import common.MessageType;

import android.os.IBinder;

/**
 * Created by lizhaoyong on 2019/9/15.
 */
public class PdaPrint {
    public static int DEVICE_MODEL = 0;
    public static IZKCService mIzkcService;
    public static String MODULE_FLAG = "module_flag";
    public static int module_flag = 0;

    private Handler mhanlder;
    public boolean bindSuccessFlag = false;

    private static PdaPrint single = null;

    private PdaPrint() {
    }

    public static PdaPrint getInstance() {
        if (single == null) {
            single = new PdaPrint();
        }
        return single;
    }

    protected void handleStateMessage(Message message) {
    }

    /**
     * handler
     */
    protected Handler getHandler() {
        if (mhanlder == null) {
            mhanlder = new Handler() {
                public void handleMessage(Message msg) {
                    handleStateMessage(msg);
                }
            };
        }
        return mhanlder;
    }

    protected void sendMessage(Message message) {
        getHandler().sendMessage(message);
    }

    protected void sendMessage(int what, Object obj) {
        Message message = new Message();
        message.what = what;
        message.obj = obj;
        getHandler().sendMessage(message);
    }

    protected void sendEmptyMessage(int what) {
        getHandler().sendEmptyMessage(what);
    }

    public void onStop() {
        if (module_flag == 3) {
            try {
                mIzkcService.openBackLight(0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("client", "onServiceDisconnected");
            mIzkcService = null;
            bindSuccessFlag = false;
            //Toast.makeText(BaseActivity.this, "service_bind_fail", Toast.LENGTH_SHORT).show();
            //发送消息绑定成功
            sendEmptyMessage(MessageType.BaiscMessage.SEVICE_BIND_FAIL);
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("client", "onServiceConnected");
            mIzkcService = IZKCService.Stub.asInterface(service);
            if (mIzkcService != null) {
                try {
                    // Toast.makeText(BaseActivity.this, "service_bind_success", Toast.LENGTH_SHORT).show();
                    DEVICE_MODEL = mIzkcService.getDeviceModel();
                    mIzkcService.setModuleFlag(module_flag);
                    if (module_flag == 3) {
                        mIzkcService.openBackLight(1);
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                bindSuccessFlag = true;
                //发送消息绑定成功
                sendEmptyMessage(MessageType.BaiscMessage.SEVICE_BIND_SUCCESS);
            }
        }
    };
    public static Date printExpire = new Date();

    public static void printMsg(String msg, Context ct) {
        if (PdaPrint.mIzkcService == null) {
            T.show(ct.getApplicationContext(), ct.getString(R.string.notsuportprint), 0);
        }


        //Log.d("word", msg);
        String utf8Str = msg;
        byte[] btUTF8 = new byte[0];
        try {
            btUTF8 = utf8Str.getBytes("UTF-8");
            if (printExpire != null && printExpire.getTime() > (new Date()).getTime()) {
                //打印太频繁
                T.show(ct.getApplicationContext(), ct.getString(R.string.printtofrequently), 0);
                return;
            }
            printExpire = new Date((new Date()).getTime() + 2000);

            mIzkcService.sendRAWData("print", new byte[]{0x1C, 0x43, (byte) 0xFF});
            SystemClock.sleep(100);
            //加粗模式
            //mIzkcService.sendRAWData("print", new byte[]{0x1B, 0x45, (byte) 0x01});
            //字号
            //mIzkcService.sendRAWData("print", new byte[]{0x1B, 0x4D, (byte) 0x20});
            //SystemClock.sleep(100);
            //打印
            mIzkcService.sendRAWData("print", btUTF8);
            SystemClock.sleep(100);
            //蜂鸣器提示
            //mIzkcService.sendRAWData("print", new byte[]{0x1B, 0x42, (byte) 0x0602});
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        // System.out.println("JS调用了Android的Print方法");
    }
}
